#!/bin/sh
# PCP QA Test No. 1602
# Exercise RESP proxying using key server command line tools.
#
# Copyright (c) 2019,2024 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check
. ./common.keys

_check_series
_check_key_server_version_offline

# only restart pmproxy if it was running before the QA test starts
restart_pmproxy=false
[ -n "`_get_pids_by_name pmproxy`" ] && restart_pmproxy=true

_cleanup()
{
    cd $here
    [ -n "$pmproxy_pid" ] && $signal -s TERM $pmproxy_pid
    _key_server_cluster_3nodes_stop
    if $need_restore
    then
	need_restore=false
        _restore_config $PCP_SYSCONF_DIR/pmproxy
	_restore_auto_restart pmproxy
	$restart_pmproxy && _service pmproxy restart >/dev/null 2>&1
    fi
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
signal=$PCP_BINADM_DIR/pmsignal

userid=`id -u`
username=`id -u -n`
hostname=`hostname`
machineid=`_machine_id`
domainname=`_domain_name`

need_restore=false
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_key_server()
{
    sed \
        -e "s,:$port1>,:PORT1>,g" \
    #end
}

_filter_key_server_err()
{
    sed \
    -e 's;ERR syntax error;expected error;' \
    -e 's;Parse command error.*;expected error;'
}

_stop_auto_restart pmproxy
if ! _service pmproxy stop >/dev/null 2>&1; then _exit 1; fi
_wait_pmproxy_end || _exit 1

# real QA test starts here
_save_config $PCP_SYSCONF_DIR/pmproxy
$sudo rm -f $PCP_SYSCONF_DIR/pmproxy/*
need_restore=true

# start cluster
_key_server_cluster_3nodes_start
echo "key_server_node1_port=$key_server_node1_port" >>$seq_full
echo "key_server_node2_port=$key_server_node2_port" >>$seq_full
echo "key_server_node3_port=$key_server_node3_port" >>$seq_full
echo

echo "== start pmproxy"
# start pmproxy 
cat >$tmp.conf <<EOF
[pmproxy]
pcp.enabled = true
resp.enabled = true
[pmseries]
servers = localhost:$key_server_node1_port,localhost:$key_server_node2_port,localhost:$key_server_node3_port
EOF
proxyport=`_find_free_port`
proxyopts="-p $proxyport -c $tmp.conf"
pmproxy -f -t -U $username -x $seq_full -l $tmp.pmproxy.log $proxyopts &
pmproxy_pid=$!
echo "proxyport=$proxyport" >>$seq_full
echo "pmproxy_pid=$pmproxy_pid" >>$seq_full
_wait_for_pmproxy $proxyport $tmp.pmproxy.log || exit
which netstat >/dev/null && netstat -l | grep -E "$key_server_node1_port|$key_server_node2_port|$key_server_node3_port|$proxyport" >>$seq_full
_check_key_server_ping $proxyport
echo

# check pmproxy has started and is available for requests
pmcd_wait -h localhost@localhost:$proxyport -v -t 5sec

# check key_server protocol proxying
echo "== key server cli set two keys"
$keys_cli -p $proxyport set pcpqa:$seq:one 1
$keys_cli -p $proxyport set pcpqa:$seq:ten 10
echo

# check pcp protocol proxying
echo "== pminfo get two metrics"
pminfo -f -h localhost@localhost:$proxyport sample.long.one sample.long.ten
echo

# check resp protocol proxying
echo "== key server cli get two keys"
$keys_cli -p $proxyport get pcpqa:$seq:one
$keys_cli -p $proxyport get pcpqa:$seq:ten
echo

# check resp protocol error handling
echo "== key server cli empty key set"
$keys_cli -p $proxyport keys no-such-key | _filter_key_server_err
echo
echo "== key server cli handle errors 1"
$keys_cli -p $proxyport get foo bar baz | _filter_key_server_err
echo
echo "== key server cli handle errors 2"
$keys_cli -p $proxyport set foo bar baz | _filter_key_server_err
echo
echo "== key server cli nonexistant key"
$keys_cli -p $proxyport get abc
echo

# check key distribution
echo "== key server cluster key distribution"
echo "== node1"
$keys_cli -p $key_server_node1_port keys pcpqa:*
echo "== node2"
$keys_cli -p $key_server_node2_port keys pcpqa:*
echo "== node3"
$keys_cli -p $key_server_node3_port keys pcpqa:*
echo

cat $tmp.conf >> $seq_full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -e '[P]PID|/[p]mproxy( |$)' >> $seq_full
cat $tmp.pmproxy.log >> $seq_full

echo "== all done" | tee -a $seq_full
status=0
exit
